<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-06 21:14:00
 * @Last Modified time: 2023-02-08 20:25:04
 */
namespace app\common\model\store;

use frank\base\BaseModel;
use frank\traits\ModelTrait;
use app\common\enum\EnumFactory;
use app\common\services\RegionService;

/**
 * 订单地址
 * Class OrderAddressModel
 * @package app\common\model\store
 */
class OrderAddressModel extends BaseModel
{
    use ModelTrait;

    /**
     * 数据表主键
     */
    protected $pk = 'id';

    /**
     * 模型名称
     */
    protected $name = 'store_order_address';

    /**
     * 追加字段
     * @var array
     */
    protected $append = ['region','full_address'];

    /**
     * 地区名称
     */
    public function getRegionAttr($value, $data)
    {
        $regionService = RegionService::instance();
        return [
            'province' => $regionService->getNameById($data['province_id']),
            'city' => $regionService->getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? '' : $regionService->getNameById($data['region_id']),
        ];
    }

    public function getFullAddressAttr($value, $data)
    {
         return $this['region']['province'] . $this['region']['city'] . $this['region']['region'] . $this['detail'];
    }

    /**
     * 获取完整地址
     */
    public function getFullAddress()
    {
        return $this['region']['province'] . $this['region']['city'] . $this['region']['region'] . $this['detail'];
    }

    /**
     * order_id搜索器
     */
    public function searchOrderIdAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('order_id', $value);
        } else {
            $query->where('order_id', $value);
        }
    }

}
